package com.wrupple.muba.catalogs.client.activity.process.state.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;
import com.wrupple.muba.catalogs.client.activity.process.state.CatalogSelectionPlaceInterpret;
import com.wrupple.muba.catalogs.client.module.services.logic.CatalogDescriptionService;
import com.wrupple.muba.catalogs.domain.CatalogPlace;
import com.wrupple.muba.common.client.application.DataCallback;
import com.wrupple.muba.common.domain.DesktopPlace;
import com.wrupple.muba.common.shared.StateTransition;

public class CatalogSelectionPlaceInterpretImpl implements CatalogSelectionPlaceInterpret {

	class CatalogNamesCallback extends DataCallback<Set<String>> {

		@Override
		public void execute() {
			List<DesktopPlace> regreso = new ArrayList<DesktopPlace>(
					result.size());

			for (String catalogid : result) {
				regreso.add(new CatalogPlace(catalogid, null));
			}

			onDone.setResultAndFinish(regreso);
		}

	}

	CatalogDescriptionService service;
	private StateTransition<List<DesktopPlace>> onDone;

	@Inject
	public CatalogSelectionPlaceInterpretImpl(CatalogDescriptionService service) {
		super();
		this.service = service;
	}

	@Override
	public void start(DesktopPlace parameter,
			StateTransition<List<DesktopPlace>> onDone, EventBus bus) {
		this.onDone = onDone;
		DataCallback<Set<String>> callback = new CatalogNamesCallback();
		service.loadCatalogNames(callback);
	}


}
